package launcher;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import service.RobotAgentService;
import service.ServiceConstants;

import com.agentfactory.afapl2.debugger.AFAPL2StateManagerFactory;
import com.agentfactory.afapl2.debugger.inspector.AFAPL2InspectorFactory;
import com.agentfactory.afapl2.interpreter.AFAPL2ArchitectureFactory;
//import com.agentfactory.agentspeak.debugger.AgentSpeakStateManagerFactory;
//import com.agentfactory.agentspeak.debugger.inspector.AgentSpeakInspectorFactory;
//import com.agentfactory.agentspeak.interpreter.AgentSpeakArchitectureFactory;
import com.agentfactory.platform.core.AgentCreationException;
import com.agentfactory.platform.core.DuplicateAgentNameException;
import com.agentfactory.platform.core.IAgent;
import com.agentfactory.platform.core.IArchitectureFactory;
//import com.agentfactory.platform.core.IPlatformService;
import com.agentfactory.platform.core.NoSuchArchitectureException;
import com.agentfactory.platform.core.NoSuchServiceException;
import com.agentfactory.platform.impl.DefaultAgentPlatform;
import com.agentfactory.platform.impl.PlatformServiceManager;
import com.agentfactory.platform.impl.RoundRobinTimeSliceFixedScheduler;
import com.agentfactory.service.ams.AgentManagementService;
import com.agentfactory.service.mts.http.HTTPMessageTransportService;
import com.agentfactory.visualiser.Debugger;

import com.agentfactory.agentspotter.AgentSpotter;


/**
 * Example Main class that illustrates how to deploy a basic Agent Factory
 * application.
 *
 * @author rem
 */
public class RobotLauncher {
	public static RobotAgentService roboService = new RobotAgentService();
	public static DefaultAgentPlatform platform = new DefaultAgentPlatform();
	public static void main(String[] args) {
		//Copy RobotDefinition to RoboCode
		try {
			util.FileCopier.copy("bin\\robot\\RobotAgent.class","robot", "C:\\robocode\\robots");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// Create a new agent platform with a basic name and domain
        platform.setName("test");
        platform.setDomain("ucd.ie");

        // Install a scheduling algorithm for executing the agents
        platform.setScheduler(new RoundRobinTimeSliceFixedScheduler());

        // Install and register the AFAPL2 Architecture Factory:
        // This enables support for instantiating AFAPL2 agents (i.e. agents
        // whose source code is identified by a .agent extension)
        IArchitectureFactory factory = new AFAPL2ArchitectureFactory();
        Properties props = new Properties();
        props.setProperty("TIMESLICE", "100");
        factory.configure(props);
        platform.getArchitectureService().registerArchitectureFactory(factory);
//        factory = new AgentSpeakArchitectureFactory();
//        platform.getArchitectureService().registerArchitectureFactory(factory);

        AgentSpotter profiler = new AgentSpotter(platform);
        // Set auto commit to force a database flush after every write (slower but safer)
        // profiler.setAutoCommit(true);
        profiler.start();
        
        // Get a reference to the Agent Management Service so that the default
        // agent community can be created...
        PlatformServiceManager manager = ((PlatformServiceManager) platform.getPlatformServiceManager());
        
        try {
            manager.addService(RobotAgentService.class, ServiceConstants.SERVICE_NAME);
            props = new Properties();
            props.setProperty("port", "4444");
            manager.addService(HTTPMessageTransportService.class, HTTPMessageTransportService.NAME, 0, props);

        } catch (NoSuchServiceException ex) {
            Logger.getLogger(RobotLauncher.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        // Install and start the Agent Factory Debugger
        Debugger debugger = new Debugger(platform);
        //debugger.registerInspectorFactory(new AgentSpeakInspectorFactory());
        //debugger.registerStateManagerFactory(new AgentSpeakStateManagerFactory());
        debugger.registerInspectorFactory(new AFAPL2InspectorFactory());
        debugger.registerStateManagerFactory(new AFAPL2StateManagerFactory());
        debugger.start();
        
        AgentManagementService ams = (AgentManagementService) platform.getPlatformServiceManager().getServiceByName(AgentManagementService.NAME);
        
        try {
        	// Create agents
        	IAgent agent5 = ams.createAgent("agent5", "agents/agentTest.agent");
            agent5.initialise("+event(happy)");
        	
        	IAgent agent4 = ams.createAgent("agent4", "agents/agentTest.agent");
            agent4.initialise("+event(happy)");
        	
        	IAgent agent3 = ams.createAgent("agent3", "agents/agentTest.agent");
            agent3.initialise("+event(happy)");
        	
            IAgent agent2 = ams.createAgent("agent2", "agents/agentTest.agent");
            agent2.initialise("+event(happy)");
            
            IAgent agent1 = ams.createAgent("agent1", "agents/agentTest.agent");
            agent1.initialise("+event(happy)");
            
           
            roboService = (RobotAgentService)manager.getServiceByName(ServiceConstants.SERVICE_NAME);
            

            
            roboService.startBattle();
            
        } catch (NoSuchArchitectureException ex) {
            Logger.getLogger(RobotLauncher.class.getName()).log(Level.SEVERE, null, ex);
        } catch (DuplicateAgentNameException ex) {
            Logger.getLogger(RobotLauncher.class.getName()).log(Level.SEVERE, null, ex);
        } catch (AgentCreationException ex) {
        	Logger.getLogger(RobotLauncher.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
